<template>
	<view class="main-box">
		<view class="btn-nav">
			<view class="item" @click="changeTab(0)" :class="{active:switchTab===0}">
				进行中
			</view>
			<view class="item" @click="changeTab(1)" :class="{active:switchTab===1}">
				已完成
			</view>
			<view class="item" @click="changeTab(2)" :class="{active:switchTab===2}">
				已取消
			</view>
		</view>
		<view class="bto-box">
			<view class="btn-item" v-for="item in list" :key="item.id">
				<view class="top-nav">
					<view class="label">
						<u-icon name="car-fill" color="#f6be64" size="24"></u-icon> 预约{{findDictLabel(item.type)}}
					</view>
					<view class="right" :class="{ doing: item.status === 'loading' }">
						{{ getStatusText(item.status) }}
					</view>
				</view>
				<view class="center-box" @click="toRouter('/appoint/detail/index')">
					<view class="image">
						<image class="image-i" :src="getImageByType(item.type)" mode="aspectFit"></image>
					</view>
					<view class="btn-name">
						<view class="name">车牌号：{{item.vehicleNo}}</view>
						<view class="name">月台：{{item.dockName || '加载中...'}}</view>
						<view class="name">排队号：{{item.num || '103'}}</view>
					</view>
				</view>
				<view class="bottom-nav">
					<view class="btn">
						去签到
					</view>
					<view class="btn danger">
						取消预约
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		mine
	} from "../../api/mobile/reservation.js"
	import {
		listByType
	} from "@/api/mobile/dict.js"
	import {
		dockById
	} from "@/api/mobile/dock.js"
	export default {
		data() {
			return {
				status: 0,
				switchTab: 0,
				form: {},
				verifyForm: {
					status: 0,
					ext: ""
				},
				list: [],
				dcitType: 'appointment_type',
				dictS: []
			}
		},
		onLoad() {
			this.driverId = uni.getStorageSync('carId')
			this.driverInfo = uni.getStorageSync('carInfo')
			this.getMine()
			this.getDict()
		},
		methods: {
			changeTab(item) {
				this.switchTab = item
			},
			async getDict() {
				let res = await listByType(this.dcitType);
				this.dictS = res.data || []
				console.log("字典数据：", this.dictS);
			},
			findDictLabel(dictValue) {
				if (!this.dictS.length) return ''
				const item = this.dictS.find(s => s.dictValue == dictValue)
				return item ? item.dictLabel : '未知类型'
			},
			getImageByType(type) {
				const typeStr = this.findDictLabel(type)
				if (typeStr.includes('卸货')) {
					return '../../static/xh.png'
				} else if (typeStr.includes('装货')) {
					return '../../static/zh.png'
				}
				return '../../static/zh.png'
			},
			getStatusText(status) {
				const statusMap = {
					'waiting': '排队中',
					'loading': '装货中',
					'unloading': '卸货中',
					'completed': '已完成',
					'cancelled': '已取消'
				}
				return statusMap[status] || '待处理'
			},
			async getDockName(dockId) {
				if (!dockId) return '未知月台'
				try {
					let res = await dockById(dockId)
					console.log("月台信息：", res)
					return res.data.locationDesc || '未知月台'
				} catch (error) {
					console.error("获取月台信息失败：", error)
					return '未知月台'
				}
			},
			goHome() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			toRouter(item) {
				uni.navigateTo({
					url: item
				})
			},
			async getMine() {
				try {
					let res = await mine(uni.getStorageSync('carId'))
					this.list = res.data || []
					console.log('获取我的预约记录', this.list)

					// 为每个预约项获取月台名称
					for (let item of this.list) {
						item.dockName = await this.getDockName(item.dockId)
					}

					// 强制更新视图
					this.$forceUpdate()
				} catch (error) {
					console.error('获取预约记录失败：', error)
				}
			}
		},
	}
</script>

<style scoped lang="scss">
	.main-box {
		width: 100%;
		min-height: 100vh;
		background: #fff;


		.btn-nav {
			width: 100%;

			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 10rpx;
			padding-bottom: 40rpx;
			background: #fff;

			.item {
				width: 100%;
				height: 60rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30rpx;
				color: #999;

				&.active {
					color: #000;
					position: relative;

					&:before {
						content: "";
						position: absolute;
						width: 60rpx;
						height: 8rpx;
						background: #ff9900;

						border-radius: 10rpx;
						top: 60rpx;
					}
				}
			}
		}

		.bto-box {
			padding: 0 30rpx 40rpx 30rpx;
			box-sizing: border-box;

			.btn-item {

				background: #fff;
				width: 100%;
				min-height: 200rpx;
				border-radius: 20rpx;
				// border: 1rpx solid #f2f2f2;
				box-shadow: rgba(99, 99, 99, 0.2) 0px 2px 8px 0px;

				margin-top: 25rpx;

				.top-nav {
					padding: 20rpx;
					box-sizing: border-box;
					width: 100%;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.label {
						font-size: 28rpx;
						color: #333;
						width: 300rpx;
						display: flex;
						align-items: center;
						font-weight: 600;
					}

					.right {
						color: #ff9900;
						font-size: 24rpx;
						font-weight: 800;

						&.doing {
							color: #19be6b;
						}
					}
				}

				.center-box {
					width: 100%;
					min-height: 80rpx;
					margin-top: 20rpx;
					display: flex;
					align-items: center;
					margin-left: 30rpx;
					padding-bottom: 20rpx;

					.image {
						height: 100rpx;

						.image-i {
							width: 90rpx;
							height: 90rpx;
						}
					}

					.btn-name {
						width: 400rpx;
						min-height: 100rpx;
						margin-left: 30rpx;

						.name {
							font-size: 28rpx;
							font-weight: 600;
						}
					}
				}

				.bottom-nav {
					width: 100%;
					display: flex;
					justify-content: right;
					background: #fff6df;
					height: 80rpx;
					border-radius: 0 0 20rpx 20rpx;
					align-items: center;
					padding: 0 30rpx;
					box-sizing: border-box;

					.btn {
						width: 140rpx;
						height: 50rpx;
						border-radius: 10rpx;
						background: #19be6b;
						color: #fff;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 26rpx;

						&.danger {
							margin-left: 10rpx;
							background: #ff0000;
						}
					}
				}
			}

		}
	}
</style>